import notification from '@kuiper-components/notification';
import fetch from 'fermionjs/lib/fetch';

export function removeTask({ taskId, target, context }) {
	fetch('/api/v2/tasks/delete', {
		method: 'POST',
		body: JSON.stringify({ ids: taskId }),
	})
		.then(res => {
			if (res?.code === 200) {
				notification.success({
					message: `删除${target}监测任务成功`,
					placement: 'top',
				});
				context.refetch();
			} else {
				notification.error({
					message: `删除${target}监测任务失败`,
					placement: 'top',
				});
			}
		})
		.catch(() => {
			notification.error({
				message: `删除${target}监测任务失败`,
				placement: 'top',
			});
		});
}

export function operateTask({ taskId, type, target, context }) {
	const url = type === 'retry' ? '/api/v2/tasks/retry' : '/api/v2/tasks/stop';
	fetch(url, {
		method: 'POST',
		body: JSON.stringify({ ids: taskId }),
	})
		.then(res => {
			if (res?.code === 200) {
				context.refetch();
			} else {
				notification.error({
					message: `${target}${type === 'retry' ? '重试' : '停止'}任务失败`,
					placement: 'top',
				});
			}
		})
		.catch(() => {
			notification.error({
				message: `${target}${type === 'retry' ? '重试' : '停止'}任务失败`,
				placement: 'top',
			});
		});
}
